export interface CronItem {
  type: string
  intervalStart?: number
  interval?: number
  rangeStart?: number
  rangeEnd?: number
  specific?: Array<number | string>
  nearestWeekday?: number
  beforeEndMonth?: number
  nthWeek?: number
  dayOfWeek?: number // Linux和Spring的允许值为0-7，0和7为周日   Quartz的允许值为1-7，1为周日
  lastDayOfWeek?: number
}
export enum CronBusKeyEnum {
  DAY = 'cron-bus-day-change',
  WEEK = 'cron-bus-week-change',
}

export function format(key: string, item: CronItem) {
  switch (key) {
    case '*':// 通配
    case '?':// 不指定
      return key
    case 'interval':// 间隔
      return `${item.intervalStart}/${item.interval}`
    case 'range':// 周期范围
      return `${item.rangeStart}-${item.rangeEnd}`
    case 'specific':// 指定
      return item.specific?.sort((a, b) => Number(a) - Number(b))?.join(',')
    case 'nearestWeekday':// 当月某日最近的那个工作日
      return `${item.nearestWeekday}W`
    case 'beforeEndMonth': // 月底前几日
      return `L-${item.beforeEndMonth}`
    case 'lastDay':// 当月的最后一日
      return 'L'
    case 'nthWeekDayOfWeek':// 当月第几周星期几
      return `${item.nthWeek}#${item.dayOfWeek}`
    case 'lastDayOfWeek':// 当月的最后一个星期几
      return `${item.lastDayOfWeek}L`
    default:
      return key
  }
}

export function useCornFormat(options: CronItem) {
  return computed(() => {
    return format(options.type, options)
  })
}
